<?
/** UCMember Handle class
*	Date:	2011-08-18
*	Author:	terrywu
*/

class UCMember{	// class start

	private	$db;						# member  db
	private $ucdb;						# ucenter db
	private $password = 'ucenter123';	# default password
	private $table;

	public function __construct($param=array()){
		global $siteMemberTable;		
		$this->db = isset($param['db'])? $param['db'] : '';
		$this->ucdb = isset($param['ucdb'])? $param['ucdb'] : '';
		$this->table = isset($siteMemberTable['main'])? $siteMemberTable['main'] : 'member';
	}


	/** 註冊Ucenter,Discuz
	* @param	int		$mid		member表id
	* @param	String	$avatar		頭像路徑
	* @return	Array
	*/
	public function reg($mid=0,$avatar=''){
		$member = db_query_one($this->table,array("where"=>"mid='".$mid."'"),$this->db,'write');
		$member = $member['result'];

		if($member){
			/* cdb_uc_members */
			$in_data = array();
			$in_data['uid'] = $mid;
			$in_data['username'] = $member['username'];
			$in_data['salt'] = randomKey(6);
			$in_data['password'] = md5(md5($this->password).$in_data['salt']);
			$in_data['email'] = $member['email'];
			$in_data['regdate'] = time();
			$cdb_uc_members	= db_add('cdb_uc_members',$in_data,$this->ucdb);

			/* cdb_uc_memberfields */
			$in_data = array();
			$in_data['uid'] = $mid;
			$in_data['blacklist'] = '';
			$cdb_uc_memberfields = db_add('cdb_uc_memberfields',$in_data,$this->ucdb);

			/* cdb_members */
			$in_data = array();
			$in_data['uid'] = $mid;
			$in_data['username'] = $member['username'];
			$in_data['gender'] = $member['gender'];
			$in_data['groupid'] = 10;
			$in_data['regdate'] = time();
			$in_data['email'] = $member['email'];
			$in_data['bday'] = $member['birth'];
			$in_data['customshow'] = 26;
			$cdb_members = db_add('cdb_members',$in_data,$this->ucdb);

			/* cdb_memberfields */
			$in_data = array();
			$in_data['uid'] = $mid;
			$cdb_memberfields = db_add('cdb_memberfields',$in_data,$this->ucdb);

			if($cdb_uc_members && $cdb_members){
				$this->save_ucenter_avatar($mid,$avatar);
				return $this->response(true);
			}else{
				return $this->response(false,'202');
			}
		}else{
			return $this->response(false,'201');
		}
	}


	/** 更新ucenter
	* @param	int		$mid	member表id
	* @return	Array
	*/
	public function update($mid=0){
		$member = db_query_one($this->table,array("where"=>"mid='".$mid."'"),$this->db,'write');
		$member = $member['result'];

		if($member){
			/* cdb_uc_members */
			$up_data = array();
			$up_data['username'] = $member['username'];
			$up_data['email'] = $member['email'];
			$condition = "uid='".$mid."'";
			$cdb_uc_members	= db_update('cdb_uc_members',$up_data,$condition,$this->ucdb);

			/* cdb_members */
			$up_data = array();
			$up_data['username'] = $member['username'];
			$up_data['gender'] = $member['gender'];
			$up_data['email'] = $member['email'];
			$up_data['bday'] = $member['birth'];
			$condition = "uid='".$mid."'";
			$cdb_members = db_update('cdb_members',$up_data,$condition,$this->ucdb);

			return $this->response(true);
		}else{
			return $this->response(false,'201');
		}
	}


	/** 輸出信息 */
	private function response($status='',$info=''){
		return array($status,$info);
	}


	/** 保存ucenter頭像
	* @param	int			$uid		ucenter id
	* @param	String		$avatar		頭像絕對路徑
	* @return	boolean
	*/
	private function save_ucenter_avatar($uid,$avatar){
		if(!empty($uid) && !empty($avatar)){
			if(!file_exists($avatar)){	# $avatar not exists
				return false;
			}
			$avatar_size = getimagesize($avatar);
			//big
			$uc_avatar_big = UC_AVATAR_PATH.'uc_server/data/avatar/'.$this->get_ucenter_avatar($uid, 'big');
			createDirs(dirname($uc_avatar_big));
			if($avatar_size[0]>200 || $avatar_size[1]>250){
				exec("convert -define jpeg:size=0x0 '".$avatar."' -thumbnail '200x250 >' -background white -gravity center -extent 200x250 '".$uc_avatar_big."'");
			}else{
				exec("convert '".$avatar."' -colorspace rgb -quality 100  '".$uc_avatar_big."' ");
			}
			//middle
			$uc_avatar_middle = UC_AVATAR_PATH.'uc_server/data/avatar/'.$this->get_ucenter_avatar($uid, 'middle');
			createDirs(dirname($uc_avatar_middle));
			if($avatar_size[0]>120 || $avatar_size[0]>120){
				exec("convert -define jpeg:size=0x0 '".$avatar."' -thumbnail '120x120 >' -background white -gravity center -extent 120x120 '".$uc_avatar_middle."'");
			}else{
				exec("convert '".$avatar."' -colorspace rgb -quality 100  '".$uc_avatar_middle."' ");
			}
			$uc_avatar_small = UC_AVATAR_PATH.'uc_server/data/avatar/'.$this->get_ucenter_avatar($uid, 'small');
			createDirs(dirname($uc_avatar_small));
			exec("convert -define jpeg:size=0x0 '".$avatar."' -thumbnail '48x48 >' -background white -gravity center -extent 48x48 '".$uc_avatar_small."'");
		}
	}


	/** 得到ucenter頭像路徑 */
	private function get_ucenter_avatar($uid, $size = 'middle', $type = '') {
		$size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
		$uid = abs(intval($uid));
		$uid = sprintf("%09d", $uid);
		$dir1 = substr($uid, 0, 3);
		$dir2 = substr($uid, 3, 2);
		$dir3 = substr($uid, 5, 2);
		$typeadd = $type == 'real' ? '_real' : '';
		return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
	}

}	// class end
?>